package cn.lystudio.rohmtimeopwide_background_v11.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.UsmBtRecordDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.UsersDepartmentPO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.UsersPO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.UsmBtRecordPO;
import cn.lystudio.rohmtimeopwide_background_v11.mapper.UsersDepartmentPOMapper;
import cn.lystudio.rohmtimeopwide_background_v11.mapper.UsersPOMapper;
import cn.lystudio.rohmtimeopwide_background_v11.mapper.UsmBtRecordPOMapper;
import cn.lystudio.rohmtimeopwide_background_v11.service.UsmBtRecordService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
@DS("master")

public class UsmBtRecordServiceImpl implements UsmBtRecordService {
    @Resource
    private UsmBtRecordPOMapper usmBtRecordPOMapper;
    @Resource
    private UsersDepartmentPOMapper usersDepartmentPOMapper;
    @Resource
    private UsersPOMapper usersPOMapper;
    @Override
    @DSTransactional
    public int addRecord(UsmBtRecordPO usmBtRecordPO) {
        LambdaQueryWrapper<UsmBtRecordPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UsmBtRecordPO::getMachineNo,usmBtRecordPO.getMachineNo())
                //.eq(UsmBtRecordPO::getLotNo,usmBtRecordPO.getLotNo())
                .eq(UsmBtRecordPO::getLotEndFlag,false)
                .orderByDesc(UsmBtRecordPO::getRecordTimeDT)
                .last("LIMIT 1");
        UsmBtRecordPO record0 = usmBtRecordPOMapper.selectOne(wrapper);
        if(ObjectUtil.isEmpty(record0)) return usmBtRecordPOMapper.insert(usmBtRecordPO);
        usmBtRecordPO.setRecordID(record0.getRecordID());
        return usmBtRecordPOMapper.updateById(usmBtRecordPO);
    }

    @Override
    public List<UsmBtRecordDTO> getList(String selectedMachineNo, String selectedDateFrom,String selectedDateTo) {
        //1.先查找半田的用户与department
        List<UsersDepartmentPO> usersDepartmentPOList = usersDepartmentPOMapper.selectList(new LambdaQueryWrapper<UsersDepartmentPO>().eq(UsersDepartmentPO::getTopDeptID, 146));
        if(CollUtil.isEmpty(usersDepartmentPOList)) return null;
        List<Integer> userIDList =CollUtil.list(false);
        CollUtil.forEach(usersDepartmentPOList.iterator(),(item,index)->{
            userIDList.add(item.getUserID());
        });
        //2.查找用户
        List<UsersPO> usersPOList = usersPOMapper.selectList(new LambdaQueryWrapper<UsersPO>().in(UsersPO::getUserID, userIDList));
        if(CollUtil.isEmpty(usersPOList)) return null;
        //3.查找数据
        List<UsmBtRecordPO> usmBtRecordList = usmBtRecordPOMapper.selectList(new LambdaQueryWrapper<UsmBtRecordPO>().eq(UsmBtRecordPO::getMachineNo, selectedMachineNo).between(UsmBtRecordPO::getRecordTimeDT, selectedDateFrom,selectedDateTo));
        if(CollUtil.isEmpty(usmBtRecordList)) return  null;
        //4.组装view层数据
        List<UsmBtRecordDTO> usmBtRecordDTOList= CollUtil.list(false);
        CollUtil.forEach(usmBtRecordList.iterator(),(usmBtRecord,index)->{
            UsmBtRecordDTO usmBtRecordDTO = new UsmBtRecordDTO();
            usmBtRecordDTO.setUsmBtRecord(usmBtRecord);
            if(StrUtil.isNotEmpty(usmBtRecord.getYmcCheck())){
               usmBtRecordDTO.setYmcCheckUser(
                       CollUtil.findOne(usersPOList,(user)->{
                           return user.getUserCode().equals(usmBtRecord.getYmcCheck());
                       })
               );
            }
            usmBtRecordDTOList.add(usmBtRecordDTO);
        });

        return usmBtRecordDTOList;
    }

    @Override
    public UsmBtRecordPO getRecordUnfinished(UsmBtRecordPO usmBtRecordPO) {
        LambdaQueryWrapper<UsmBtRecordPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UsmBtRecordPO::getMachineNo,usmBtRecordPO.getMachineNo())
                //.eq(UsmBtRecordPO::getLotNo,usmBtRecordPO.getLotNo())
                .eq(UsmBtRecordPO::getLotEndFlag,false)
                .orderByDesc(UsmBtRecordPO::getRecordTimeDT)
                .last("LIMIT 1");
        return usmBtRecordPOMapper.selectOne(wrapper);
    }

    @Override
    public  List<UsmBtRecordDTO> getFinishedRecordByLotNo(String lotNo) {
        //1.先查找半田的用户与department
        List<UsersDepartmentPO> usersDepartmentPOList = usersDepartmentPOMapper.selectList(new LambdaQueryWrapper<UsersDepartmentPO>().eq(UsersDepartmentPO::getTopDeptID, 146));
        if(CollUtil.isEmpty(usersDepartmentPOList)) return null;
        List<Integer> userIDList =CollUtil.list(false);
        CollUtil.forEach(usersDepartmentPOList.iterator(),(item,index)->{
            userIDList.add(item.getUserID());
        });
        //2.查找用户
        List<UsersPO> usersPOList = usersPOMapper.selectList(new LambdaQueryWrapper<UsersPO>().in(UsersPO::getUserID, userIDList));
        if(CollUtil.isEmpty(usersPOList)) return null;
        //3.查找数据
        LambdaQueryWrapper<UsmBtRecordPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(UsmBtRecordPO::getLotNo,lotNo)
                .eq(UsmBtRecordPO::getLotEndFlag,true);
        List<UsmBtRecordPO> usmBtRecordList = usmBtRecordPOMapper.selectList(wrapper);
        if(CollUtil.isEmpty(usmBtRecordList)) return null;
        //4.组装view层数据
        List<UsmBtRecordDTO> usmBtRecordDTOList= CollUtil.list(false);
        CollUtil.forEach(usmBtRecordList.iterator(),(usmBtRecord,index)->{
            UsmBtRecordDTO usmBtRecordDTO = new UsmBtRecordDTO();
            usmBtRecordDTO.setUsmBtRecord(usmBtRecord);
            if(StrUtil.isNotEmpty(usmBtRecord.getYmcCheck())){
                usmBtRecordDTO.setYmcCheckUser(
                        CollUtil.findOne(usersPOList,(user)->{
                            return user.getUserCode().equals(usmBtRecord.getYmcCheck());
                        })
                );
            }
            usmBtRecordDTOList.add(usmBtRecordDTO);
        });
        return usmBtRecordDTOList;
    }

    @Override
    public UsmBtRecordPO getUsmBtRecordByID(String recordID) {
        LambdaQueryWrapper<UsmBtRecordPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UsmBtRecordPO::getRecordID,Integer.parseInt(recordID))
                .eq(UsmBtRecordPO::getLotEndFlag,true)
                .last("LIMIT 1");

        return usmBtRecordPOMapper.selectOne(wrapper);
    }

    @Override
    public int uptRecord(UsmBtRecordPO usmBtRecordPO) {
        return usmBtRecordPOMapper.updateById(usmBtRecordPO);
    }

    @Override
    public int delRecord(Integer recordID) {
        return usmBtRecordPOMapper.deleteById(recordID);
    }
}
